package com.soft.idc.common.control;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.ICallback;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.soft.idc.common.model.IdcUser;
import com.soft.idc.common.service.IdcAuthTemplateService;
import com.soft.idc.common.service.IdcOrgService;
import com.soft.idc.common.util.JQPage;

public class RoleManagerController extends Controller {
	public void index() {
		this.render("/role/role.html");
	}

	public void queryRole() {
		int start = this.getParaToInt("start");
		int length = this.getParaToInt("length");
		IdcUser u = this.getSessionAttr("SYS_USER");
		String role_name = this.getPara("role_name");
		String org_name = this.getPara("org_name");
		String level = this.getPara("level");
		String auth_template_name = this.getPara("auth_template_name");
		StringBuffer sb = new StringBuffer();
		sb.append(
				"SELECT R.ORG_ID, R.AUTH_ID, R.ROLE_ID, R.ROLENAME, CASE(R.LEVEL) WHEN 1 THEN '一级' WHEN 2 THEN '二级' WHEN 3 THEN '三级' ELSE '' END AS LEVEL, ");
		sb.append(" O.NAME AS ORG_NAME, A.NAME AS AUTH_NAME  ");
		StringBuffer sqlFrom = new StringBuffer();
		sqlFrom.append(" FROM IDC_ROLE R LEFT JOIN IDC_ORG O ON O.ORG_ID = R.ORG_ID  ");
		sqlFrom.append(" LEFT JOIN IDC_AUTH_TEMPLATE A ON R.AUTH_ID = A.AUTH_ID where 1=1 ");
		if (role_name != null) {
			sqlFrom.append(" AND R.ROLENAME LIKE '%" + role_name + "%' ");
		}

		if (org_name != null) {
			sqlFrom.append(" and O.NAME like '%" + org_name + "%' ");
		}
		if (level != null) {
			int levelInt = this.getParaToInt("level");
			sqlFrom.append(" AND R.LEVEL =" + levelInt + " ");
		}

		if (auth_template_name != null) {
			sqlFrom.append(" and A.NAME like '%" + auth_template_name + "%' ");
		}
		int pageNum = start / length + 1;

		Page<Record> page = Db.paginate(pageNum, length, sb.toString(), sqlFrom.toString());

		JQPage jPage = new JQPage(page);

		this.renderJson(jPage);
	}

	public void queryByCond() {
		String roleName = this.getPara("roleName");
		String level = this.getPara("level");
		String orgName = this.getPara("orgName");
		String authId = this.getPara("authId");
	}

	public void add() {
		String roleName = this.getPara("roleName");
		String level = this.getPara("level");
		String org_id = this.getPara("org_id");
		String auth_template_id = this.getPara("auth_template_id");
		String sql = "INSERT INTO IDC_ROLE (ROLENAME, LEVEL, ORG_ID, AUTH_ID) VALUES (? , ? , ?, ?)";
		int result = Db.update(sql, new Object[] { roleName, level, org_id, auth_template_id });
		this.renderJson(result);

	}

	public void delete() {
		final String roleIds = this.getPara("roleIds");
		ICallback call = new ICallback() {

			@Override
			public Object call(Connection conn) throws SQLException {
				// TODO Auto-generated method stub
				conn.setAutoCommit(false);
				String[] roleIdArr = roleIds.split(";");
				StringBuffer sb = new StringBuffer();
				sb.append("DELETE FROM IDC_ROLE WHERE ROLE_ID IN(");
				for (int i = 0; i < roleIdArr.length; i++) {
					if (i == roleIdArr.length - 1) {
						sb.append(roleIdArr[i] + ")");
					} else {
						sb.append(roleIdArr[i] + ",");
					}
				}
				Statement sta = conn.createStatement();
				int count = sta.executeUpdate(sb.toString());
				if (count == roleIdArr.length) {
					conn.commit();
					conn.close();
					return true;
				} else {
					conn.rollback();
					conn.close();
					return false;
				}
			}
		};
		Object obj = Db.execute(call);
		this.renderJson(obj);
	}

	public void edit() {
		String roleId = this.getPara("roleId");
		String roleName = this.getPara("roleName");
		String level = this.getPara("level");
		String org_id = this.getPara("org_id");
		String auth_template_id = this.getPara("auth_template_id");
		String sql = "UPDATE IDC_ROLE SET ROLENAME=? , LEVEL=? ,ORG_ID=? , AUTH_ID=? WHERE ROLE_ID=?";
		int result = Db.update(sql, new Object[] { roleName, level, org_id, auth_template_id, roleId });
		this.renderJson(result);
	}

	public void queryAuthTemplate() {
		List<Record> result = IdcAuthTemplateService.loadAuthTemplate();
		this.renderJson(result);
	}

	public void queryOrgList() {
		List<Record> result = IdcOrgService.loadOrg();
		this.renderJson(result);
	}
}
